import StoreShop from "../module/StoreShop";
import { ActionType } from "../../index";
import _ from "lodash";

const NativeStoreMallModule = require('../../../core/modules/NativeStoreMallModule');

/**
 * 获取商城布局和数据接口。这里需要针对数据进行整合
 * @param componentId 商城唯一识别
 * @param data 入参数据
 * @returns
 */
export function init(componentId: string, data: StoreShop) {
  return async (dispatch: any) => {
    if (data) {
      dispatch({
        type: ActionType.STORE_SHOP_INIT,
        componentId,
        data: data.getData(),
        layout: data.getLayout(),
      });
    }
  };
}

export function cardCheck(componentId: string, data: any) {
  return async (dispatch: any, getState: any) => {

    let map = await NativeStoreMallModule.openDialog();

    console.log("[内部选择的回调]",map)

    let shopCard = findValueByKey(getState,componentId,"shopCard");
    if (!shopCard) {
      shopCard = [];
    }
    shopCard.push(data);
    if (data) {
      dispatch({
        type: ActionType.STORE_SHOP_CARD_CHECK,
        componentId,
        shopCard: _.cloneDeep(shopCard),
      });
    }
  };
}


//根据提供的key从state中获取对应数据
export function findValueByKey(
  getState: any,
  componentId: string,
  key: string
) {
  let value = null;
  if (getState) {
    value = getState().getIn(["reducer", "instances", componentId, key]);
  }
  return value;
}